package com.brooks.demo;

/**
 * @author Maoyan
 * @data 2020/10/17 16:27
 * @Version 1.0.0
 */
public class Race implements Runnable {
    private static String winner;
    public void run() {
        for (int i = 0; i <= 100; i++) {

            boolean flag = gameOver(i);
            if (flag) {
                break;
            }else {
                if("兔子".equals(Thread.currentThread().getName())){
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");


            }

        }
    }
    //判断跑步是否结束
    private boolean gameOver(int steps){
        if (winner != null){
            return true;
        }else {
            if (steps>=100){
                winner = Thread.currentThread().getName();
                System.out.println("胜利者是："+winner);
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Race race = new Race();
        new Thread(race,"兔子").start();
        new Thread(race,"乌龟").start();

    }
}
